package com.himiotech.core.project.web.system;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.anno.Log;
import com.himiotech.core.framework.helper.enums.BusinessType;
import com.himiotech.core.framework.helper.utils.ExcelUtil;
import com.himiotech.core.project.model.entity.Notice;
import com.himiotech.core.project.model.metadata.NoticeMetadata;
import com.himiotech.core.project.service.INoticeService;
import com.himiotech.core.project.web.base.HimioBaseController;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 通知公告表 web层
 *
 * @author wuwenbin
 */
@Controller
@RequestMapping("/system/notice")
public class NoticeController extends HimioBaseController {

    @Autowired
    private INoticeService noticeService;

    @RequiresPermissions("system:notice:view")
    @GetMapping
    public String notice() {
        return "system/notice";
    }

    /**
     * 查询通知公告表列表
     *
     * @param page   分页信息
     * @param notice 查询条件
     * @return 表格分页数据
     */
    @RequiresPermissions("system:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public HimioResult list(Page<Notice> page, Notice notice) {
        handleOrderBy(request, page);
        page = noticeService.selectNoticeListPage(page, notice);
        return writeJsonLayuiTable(page);
    }

    /**
     * 导出通知公告表列表数据
     *
     * @param notice 查询条件
     * @return 导出数据
     */
    @Log(title = "通知公告表 - 导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:notice:export")
    @PostMapping("/export")
    @ResponseBody
    public HimioResult export(Notice notice) {
        QueryWrapper<Notice> query = Wrappers.query();
        if (notice != null) {
            query.eq(ObjectUtil.isNotEmpty(notice.getNoticeTitle()), NoticeMetadata.NOTICE_TITLE.getColName(), notice.getNoticeTitle());
            query.eq(ObjectUtil.isNotEmpty(notice.getNoticeType()), NoticeMetadata.NOTICE_TYPE.getColName(), notice.getNoticeType());
            query.like(ObjectUtil.isNotEmpty(notice.getCreateByName()), NoticeMetadata.CREATE_BY_NAME.getColName(), notice.getCreateByName());

        }
        List<Notice> list = noticeService.list(query);
        ExcelUtil<Notice> util = new ExcelUtil<>(Notice.class);
        return util.exportExcel(list, "通知公告表");
    }


    /**
     * 新增通知公告表
     *
     * @param notice 新增对象
     * @return 处理结果
     */
    @RequiresPermissions("system:notice:add")
    @Log(title = "通知公告表 - 新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public HimioResult addSave(@Validated Notice notice) {
        return writeJson(noticeService.save(notice));
    }


    /**
     * 修改通知公告表
     *
     * @param notice 修改对象
     * @return 处理结果
     */
    @RequiresPermissions("system:notice:edit")
    @Log(title = "通知公告表 - 修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public HimioResult editSave(@Validated Notice notice) {
        return writeJson(noticeService.updateById(notice));
    }

    /**
     * 删除通知公告表
     *
     * @param ids 删除id（以逗号分隔）
     * @return 处理结果
     */
    @RequiresPermissions("system:notice:remove")
    @Log(title = "通知公告表 - 删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public HimioResult remove(String ids) {
        if (ids != null) {
            List<String> idList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
            return writeJson(noticeService.removeByIds(idList));
        } else {
            return writeJsonOk("没有需要删除的数据");
        }
    }


}

